package com.idconfirmsystem.remote;

import java.io.IOException;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kymjs.kjframe.http.HttpCallBack;
import org.xmlpull.v1.XmlPullParserException;

import com.idconfirmsystem.Conf;

public class SoapHelper {
	public void get(String serviceName, String methodName, Map<String, String> mProperties, HttpCallBack httpCallback) {
		// 命名空间
		String nameSpace = "zyznkj";
		// EndPoint
		String endPoint = Conf.ServiceConf.getServiceUrl() + serviceName + "." + serviceName + "HttpSoap11Endpoint/";
		// SOAP Action
		String soapAction = Conf.ServiceConf.getServiceUrl() + serviceName + "/";
		// 指定WebService的命名空间和调用方法
		SoapObject soapObject = new SoapObject(nameSpace, methodName);
		// 设置需要调用WebService接口的两个参数mobileCode UserId
		if (mProperties != null) {
			for (Map.Entry<String, String> entry : mProperties.entrySet()) {
				soapObject.addProperty(entry.getKey(), entry.getValue());
			}
		}
		// 生成调用WebService方法调用的soap信息，并且指定Soap版本
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
		envelope.bodyOut = soapObject;
		// 是否调用DotNet开发的WebService
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE transport = new HttpTransportSE(endPoint);
		try {
			transport.call(soapAction, envelope);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			httpCallback.onFailure(e.hashCode(), e.getMessage());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			httpCallback.onFailure(e.hashCode(), e.getMessage());
		}
		// 获取返回的数据
		SoapObject object = (SoapObject) envelope.bodyIn;
		// 获取返回的结果
		String result = object.getProperty(0).toString();
		httpCallback.onSuccess(result);
	}
}
